var lists;
var selectedListName = "";

function createList(listname) {
    var list = new Object();
    list.name = listname;
    list.tasks = new Array();
    if(typeof(lists)=="undefined" || lists===null) {
        lists = new Array();
    }
    lists.push(list);
    refreshListsModel();
}

function createTask(taskname) {
    var task = new Object();
    task.name = taskname;
    task.done = false;
    var list = getSelectedList();
    if(typeof(list.tasks)=="undefined") {
        list.tasks = new Array();
    }
    list.tasks.push(task);
    fillTasksList(list.name);
}

function purge() {
    var list = getSelectedList();
    var hasDone;
    do {
        hasDone = false;
        for(var taskIndex in list.tasks) {
            if(list.tasks[taskIndex].done) {
                hasDone = true;
                list.tasks.splice(taskIndex,1);
                break;
            }
        }
    } while(hasDone);
    fillTasksList(selectedListName);
}

function getSelectedList() {
    for(var listIndex in lists) {
        var list = lists[listIndex];
        if(list.name == selectedListName) {
            return list;
        }
    }
    return null;
}

function refreshListsModel() {
    listsModel.clear();
    for(var listIndex in lists) {
        var list = lists[listIndex];
        listsModel.append({"name":list.name});
    }
}

function deleteTask(taskName) {
    for(var listIndex in lists) {
        var list = lists[listIndex];
        if(list.name == selectedListName) {
            for(var taskIndex in list.tasks) {
                var task = list.tasks[taskIndex];
                if(task.name == taskName) {
                    console.log("Remove task " + taskIndex);
                    list.tasks.splice(taskIndex,1);
                    break;
                }
            }
            break;
        }
    }
    fillTasksList(selectedListName);
}

function removeCurrentList() {
    for(var listIndex in lists) {
        var list = lists[listIndex];
        if(list.name == selectedListName) {
            lists.splice(listIndex,1);
            break;
        }
    }
    refreshListsModel();
}

function renameTask(originalName, newName) {
    for(var listIndex in lists) {
        var list = lists[listIndex];
        if(list.name == selectedListName) {
            for(var taskIndex in list.tasks) {
                var task = list.tasks[taskIndex];
                if(task.name == originalName) {
                    task.name = newName;
                    break;
                }
            }
            break;
        }
    }
    fillTasksList(selectedListName);
}

function markTaskDone( index, isDone ) {
    var list = getSelectedList();
    list.tasks[index].done = isDone;
}

function fillTasksList(listName) {
    for(var listIndex in lists) {
        var list = lists[listIndex];
        if(list.name == listName) {
            tasksModel.clear();
            for(var taskIndex in list.tasks) {
                var task = list.tasks[taskIndex];
                tasksModel.append(task);
            }
            break;
        }
    }
}

function replacer(key, value) {
    if (typeof value === 'number' && !isFinite(value)) {
        return String(value);
    }
    return value;
}
